// pages/accountLogin/accountLogin.js
const db = wx.cloud.database();
var app = getApp();
var languageUtil = require('../../utils/languageUtils')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:'',
    password:'',
    loginstate:false,
  },
  //登录验证
  loginJudge(){
    var that = this
    db.collection('userList').where({
      phone:this.data.phone
    }).get({
      success:function(res){
        //验证账号是否存在，密码是否正确
        if(res.data[0]){
          that.judgeAccount(res.data[0].password)
        }else{
          wx.showModal({
            title:'错误信息',
            content:'账号不存在',
            showCancel: false
          })
        } 
        //传递参数到app.golbalData        
        if(that.data.loginstate){
          that.setGlobalData(res.data[0].nickName,res.data[0].avatarUrl)
        }
      }
    })
  },
  //判断密码是否正确
  judgeAccount(correctPassword){
    if(correctPassword === this.data.password){
      //传递参数
      this.setData({
        loginstate:true
      })
    }else{
      wx.showModal({
        title:'错误信息',
        content:'密码错误',
        showCancel: false
      })
    }
  },
  //获取头像，用户名及登录状态
  setGlobalData(nickName,fileid){
    app.globalData.userName = nickName
    app.globalData.userPic = fileid
    app.globalData.hasLogin = true
    wx.navigateBack({
      delta: 2,
    })
  },
  //获取phone-input
  getPhone:function(e){
    this.setData({
      phone:e.detail.value
    })
  },
  //获取password-input
  getPassword:function(e){
    this.setData({
      password:e.detail.value
    })
  },
  //删除input
  clearInput: function(e) {
    switch (e.currentTarget.id) {
      case 'clear-phone':
        this.setData({
          phone: ''
        });
        break;
      case 'clear-password':
        this.setData({
          password: ''
        }) ;
        break; 
    }
  },
    switchLanguage() {
      //切换当前版本，即修改公共变量中的version
      languageUtil.changLanguage()
    this.initLanguage()
    },
  //初始化语言
    initLanguage() {
      //获取当前小程序语言版本所对应的字典变量
      var lang = languageUtil.languageVersion()
      this.setData({
        content: lang
      })
    },
    /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (_options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initLanguage()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  }
})